package com.wu.creditmanager;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.wu.creditmanager.adapter.BillAdapter;
import com.wu.creditmanager.db.CreditsDbAdapter;
import com.wu.creditmanager.dialog.ActionItem;
import com.wu.creditmanager.dialog.EditDialogBuilder;
import com.wu.creditmanager.dialog.EditDialogBuilder.OnInputValueListener;
import com.wu.creditmanager.dialog.QuickActions;
import com.wu.creditmanager.entity.Bill;
import com.wu.creditmanager.util.Constants;
import com.wu.creditmanager.util.StringUtil;

public class CreditManager extends Activity implements OnItemClickListener {

	private ListView notifyView;
	private CreditsDbAdapter mDbHelper;
	private List<Bill> bills;
	private QuickActions quickActions;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.credit_main);
		notifyView = (ListView) findViewById(R.id.notify_list);
		notifyView.setEmptyView(findViewById(android.R.id.empty));
		notifyView.setOnItemClickListener(this);
		mDbHelper = new CreditsDbAdapter(this);
	}

	private void setNotify() {
		mDbHelper.open();
		bills = mDbHelper.fetchAllNotPaidBills();
		mDbHelper.close();
		notifyView.setAdapter(new BillAdapter(this, bills));
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case Constants.MENU_ABOUT:
			return new AlertDialog.Builder(this)
					.setIcon(R.drawable.icon)
					.setTitle(R.string.dialog_about_header)
					.setMessage(R.string.dialog_about)
					.setPositiveButton(R.string.btn_OK,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							}).create();
		default:
			return null;
		}
	}

	public void onItemClick(AdapterView<?> listview, View v, int position,
			long id) {
		final Bill bill = bills.get(position % bills.size());
		quickActions = new QuickActions(v);
		quickActions.addActionItem(new ActionItem(getResources().getDrawable(
				R.drawable.money), getResources().getString(
				R.string.update_amount_payable), new OnClickListener() {
			public void onClick(View v) {
				showUpdateAmountPayableDialog(bill);
			}
		}));
		quickActions.addActionItem(new ActionItem(getResources().getDrawable(
				R.drawable.money), getResources().getString(R.string.pay),
				new OnClickListener() {
					public void onClick(View v) {
						showPayDialog(bill);
					}
				}));
		quickActions.show();
	}

	private void showPayDialog(final Bill bill) {
		new EditDialogBuilder(this, R.string.entryPayAmount)
				.setInputType(
						InputType.TYPE_CLASS_NUMBER
								| InputType.TYPE_NUMBER_FLAG_SIGNED
								| InputType.TYPE_NUMBER_FLAG_DECIMAL)
				.setOnInputValueListener(new OnInputValueListener() {
					public void setValue(String value) {
						if (StringUtil.isEmpty(value)) {
							Toast.makeText(CreditManager.this,
									R.string.empty_entry, Toast.LENGTH_SHORT)
									.show();
							return;
						}
						bill.setPaid(true);
						bill.setPaytime(System.currentTimeMillis());
						bill.setPayAmount(Double.valueOf(value));
						mDbHelper.open(true);
						mDbHelper.updateBill(bill);
						mDbHelper.close();
						setNotify();
					}
				}).show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		setNotify();
	}

	private void showUpdateAmountPayableDialog(final Bill bill) {
		new EditDialogBuilder(this, R.string.entryAmountPayalbe)
				.setInputType(
						InputType.TYPE_CLASS_NUMBER
								| InputType.TYPE_NUMBER_FLAG_SIGNED
								| InputType.TYPE_NUMBER_FLAG_DECIMAL)
				.setOnInputValueListener(new OnInputValueListener() {
					public void setValue(String value) {
						if (StringUtil.isEmpty(value)) {
							Toast.makeText(CreditManager.this,
									R.string.empty_entry, Toast.LENGTH_SHORT)
									.show();
							return;
						}
						bill.setAmountPayable(Double.valueOf(value));
						mDbHelper.open(true);
						mDbHelper.updateBill(bill);
						mDbHelper.close();
						setNotify();
					}
				}).show();
	}

}